<?php

namespace haibao\cms\data\emode;

use haibao\cms\model\filter\activity\Activity as ActivityFilter;

class EmodeArticle extends \haibao\cms\data\BaseMysql{

    public function __construct(){
        parent::__construct('\haibao\cms\model\data\emode\EmodeBrand');
    }
    
    public function getEmodeArticleList($query){
       return $this->getAll($query);
    }
    
    public function saveEmodeArticle($model){
        $article = new \haibao\cms\common\ArticleContent();
        $model->Content = $article->replaceCDNLink($model->Content);
        if($model->Id){
            $this->save($model);
        }else{
            $this->add($model);
        }
    }
    
    public function getOneArticleById($id){
        $sql = 'select Id,Title,ImageId,Content,PublishTime,CreateBy,CreateTime from EmodeArticle where Id = %s and Status = %s';
        return $this->query($sql,array($id,\haibao\cms\model\data\emode\EmodeArticle::STATUS_ENABLE))->fetch_assoc();
    }
    
    public function delArticle($articleId,$status){
        $sql = 'update EmodeArticle set Status = %s where Id = %s';
        $this->query($sql,array($status,$articleId));
    }
    
    public function getEmodeArticle($page,$lastArticleId){
        $page = isset($page) ? $page :1;
        if($page!=1){
            $starCount = ($page-1) * 5;
        }else{
            $starCount = 0;
        }
        if($lastArticleId){
            $sql = 'select Id,Title,PublishTime,ImageId from EmodeArticle where Status = %s and IsPublish = %s and PublishTime < %s order by PublishTime desc limit 0,5';
            $result = $this->query($sql,array(\haibao\cms\model\data\emode\EmodeArticle::STATUS_ENABLE,\haibao\cms\model\data\emode\EmodeArticle::PUBLISHED_YES,$lastArticleId));
        }else{
            $sql = 'select Id,Title,PublishTime,ImageId from EmodeArticle where Status = %s and IsPublish = %s  order by PublishTime desc limit %s,5';
            $result = $this->query($sql,array(\haibao\cms\model\data\emode\EmodeArticle::STATUS_ENABLE,\haibao\cms\model\data\emode\EmodeArticle::PUBLISHED_YES,(int)$starCount));
        }
        $emodeArticleArr = array();
        while($row = $result->fetch_assoc()){
            $emodeArticleArr[] = $row;
        }
        return $emodeArticleArr;
    }
    

    
}